﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MetaVideoEditorEvolved.Entity;
using MetaVideoEditorEvolved.Helper;
using MetaVideoEditorEvolved.Messages;
using mveEngine;

namespace MetaVideoEditorEvolved.ViewModel
{
	public class CreateMediaFolderVM : ViewModelBase
	{
		private string path;
		private BackgroundWorker bwMoveMedia = new BackgroundWorker();
		private ObservableCollection<CreateMediaFolderItem> items = new ObservableCollection<CreateMediaFolderItem>();
		private CreateMediaFolderHelper helper;
		private  ObservableCollection<string> folderMediaList;
		
		public CreateMediaFolderVM()
		{
			this.CloseCommand = new GalaSoft.MvvmLight.Command.RelayCommand(new Action(() =>
			                                                                           {
			                                                                           	Messenger.Default.Send<CloseMessage>(new CloseMessage { WindowToClose = WindowType.CreateMediaFolderWindow });
			                                                                           }),
			                                                                () =>
			                                                                {
			                                                                	return this.bwMoveMedia!=null && !this.bwMoveMedia.IsBusy;
			                                                                });
			
			this.ExecuteCommand = new GalaSoft.MvvmLight.Command.RelayCommand(new Action(() =>
			                                                                             {
			                                                                             	bwMoveMedia.RunWorkerAsync();
			                                                                             }),
			                                                                  () =>
			                                                                  {
			                                                                  	return !string.IsNullOrEmpty(Path) &&
			                                                                  		Items != null &&
			                                                                  		Items.Any() &&
			                                                                  		this.bwMoveMedia!=null &&
			                                                                  		!this.bwMoveMedia.IsBusy;
			                                                                  });
			
			this.ChooseDirectoryCommand = new RelayCommand(()=>
			                                               {
			                                               	var fbd = new FolderBrowserDialog();
			                                               	
			                                               	if(fbd.ShowDialog() == DialogResult.OK)
			                                               	{
			                                               		Path = fbd.SelectedPath;
			                                               	}
			                                               });
			this.folderMediaList = new ObservableCollection<string>(Config.Instance.RootFolders);
			
			bwMoveMedia.DoWork += (s,e)=>
			{
				foreach (var element in Items)
				{
					helper.CreateMediaFolder(element);
				}
			};
		}
		
			
		public ObservableCollection<string> FolderMediaList
		{
			get
			{
				return this.folderMediaList;
			}
		}
		
		
		public ObservableCollection<CreateMediaFolderItem> Items
		{
			get
			{
				return items;
			}
			
			set
			{
				items =value;
				RaisePropertyChanged("Items");
			}
		}
		
		public string Path
		{
			get
			{
				return path;
			}
			
			set
			{
				if(!FolderMediaList.Contains(value))
				{
					FolderMediaList.Add(value);
				}
				
				helper = new CreateMediaFolderHelper(value);
				
				Items = new ObservableCollection<CreateMediaFolderItem>(helper.GetMediaFolder());
				
				path = value;
				RaisePropertyChanged("Path");
			}
		}
		
		public ICommand CloseCommand { get; private set; }
		
		public ICommand ChooseDirectoryCommand { get; private set; }
		
		public ICommand ExecuteCommand { get; private set; }
	}
}
